//Player following update function 
function FollowPlayer(){
    var speed = 2;
    var dx = g_player.x - this.x;
    var dy = g_player.y  - this.y;
    var deno = Math.sqrt(dx * dx + dy*dy);
    this.vx = speed * dx/deno;
    this.vy = speed * dy/deno;
    this.x += this.vx; 
    this.y += this.vy; 
    this.angle = Math.atan2(dy,dx);
}

//bouncing around update function
function BounceAround(){
    var speed = 3;
    this.angle+= 0.1;
    if(this.vx == 0 || this.vy == 0)
    {
        this.vx = this.vy = speed;
    }
    this.x += this.vx;
    this.y += this.vy;

    if(this.x < 0 || this.x > canvas.width){
        this.vx = -this.vx;
        this.x += this.vx;
    }
    if(this.y < 0 || this.y > canvas.height){
        this.vy = -this.vy;
        this.y += this.vy;
    }
}

function Enemy(x, y, image, updatefunc) //enemy class
{
    this.x = x;
    this.y = y;
    this.vx = 0; 
    this.vy = 0;
    this.health = 100; 
    this.image = image;
    this.update = updatefunc;
    this.angle = 0;
    this.damage = 2;

    this.draw = function draw() {
        var min_alpha = 0.2;
        ctx.save();
        ctx.globalAlpha=(this.health+min_alpha)/100;
        ctx.translate(this.x, this.y);
        ctx.rotate(this.angle+3.14/2); 
        //ctx.drawImage(this.image, this.x-g_size/2, this.y-g_size/2, g_size, g_size);
        ctx.drawImage(this.image, -g_size/2, -g_size/2, g_size, g_size);
        ctx.restore();
    }
}

function Player() //player class 
{
    this.x = g_mx;
    this.y = g_my;
    this.radius = 60;
    this.image = png_hero;
    this.health = 100;
    this.score = 0;
    this.new_highScore = false;
    this.damage = 2;

    this.draw = function draw(){
        ctx.fillStyle="rgba(255,255,255,0.1)";
        ctx.beginPath();
        ctx.arc(this.x, this.y, this.radius, 0, Math.PI*2, true);
        ctx.fill();
        ctx.globalAlpha = this.health / 100;
        ctx.drawImage(this.image, this.x-g_size/2, this.y-g_size/2, g_size, g_size);
        ctx.globalAlpha = 1.0;
    }

    this.isOverlap = function Overlap(obj){
        var dx = obj.x - this.x;
        var dy = obj.y - this.y;
        var norm = dx*dx + dy*dy;
        if (norm < (this.radius+g_size) * (this.radius+g_size))
        {
            if(norm < g_size * g_size)
            {//if you want to add power ups this is the place to write the code.
                this.health -= obj.damage;
                if(obj.damage < 0) //its' a power up
                {
                    snd_up.play();
                    var msg;
                    if(obj.image == png_star){
                        msg = "Quad Damage";
                        obj.health = -1;
                        this.damage = 6;
                    }
                    g_TextMessage.init(msg);
                }
                else
                {//colission with enemies removes powerup.
                    if(this.damage > 2)
                    {
                        snd_down.play();
                        this.damage = 2;
                    }                        
                    if(this.health % 5 == 0)
                        AddBlast(obj, "rgb(255,255,255)");
                }
            }
            return true;
        }
        //else 
        return false;
    }
    this.update = function update(){
        this.x = g_mx;
        this.y = g_my;
    }
}

